<?php defined('BASEPATH') or exit('No direct script access allowed');

class Category_model extends EXT_Model
{
	public $list_select = 'id, parent_id, title, alias, published, description, tag, ordering';
	public $list_filter_fields = array('id', 'ordering', 'title', 'parent_id', 'published');
	public $list_orderby = array('ordering asc', 'id desc');
	public $categories = array();
	public $root = array();
	protected $_table = 'categories';

	public function __construct()
	{
		parent::__construct();

		$categories = $this->getList();
		foreach($categories as $c1)
		{
			$category = $c1;
			$category->child = array();
			foreach($categories as $c2)
			{
				if($c2->parent_id == $c1->id)
					$category->child[] = $c2->id;
			}
			$this->categories[$c1->id] = $category;
			if($c1->parent_id == 0)
			{
				$this->root[$c1->id] = $c1;
			}
		}
	}
	public function get($id)
	{
		if(isset($this->categories[$id]))
			return $this->categories[$id];
		return array();
	}
	public function getByAlias($alias)
	{
		foreach($this->categories as $category)
		{
			if($category->alias == $alias)
			{
				return $category;
				break;
			}
		}
	}
	public function getAll()
	{
		return $this->categories;
	}
	public function getRoot()
	{
		return $this->root;
	}
}




